package com.dhcc.cdc.sys.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.cache.config.CdcConfigCache;
import com.dhcc.cdc.sys.entity.DataReceiveLogEntity;
import com.dhcc.cdc.sys.service.ICdcDataReceiveLogService;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 院区数据、科室数据、患者数据、检验数据等
 * 
 * @ClassName: CdcDataController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-31 17:43:57
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcDataReceiveLog")
public class CdcDataReceiveLogController extends BaseController {

	private String PREFIX = "/backend/cdc/datareceivelog/";

    @Autowired
    private ICdcDataReceiveLogService dataReceiveLogService;

    /**
     * 跳转到列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "datareceivelog.html";
    }

    /**
     * 获取列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        Page<DataReceiveLogEntity> page = new PageFactory<DataReceiveLogEntity>().defaultPage();
        List<DataReceiveLogEntity> result = this.dataReceiveLogService.page(page, params);
        page.setRecords((List<DataReceiveLogEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }
    /**
     * @param params
     * @Description: 查询日志详细信息
     * @Title: searchLogFieId
     * @return: Object
     * @author: wujiafeng
     * @date:2020年6月24日 上午10:50:55
     */
    @RequestMapping(value = "/fieId")
    @ResponseBody
    public Object fieId(@RequestParam Map<String, Object> params) {
        Wrapper<DataReceiveLogEntity> wrapper = new EntityWrapper<DataReceiveLogEntity>();
        wrapper.eq("id", params.get("id"));
        DataReceiveLogEntity dataReceiveLog = dataReceiveLogService.selectOne(wrapper);
        if("message".equals((String)params.get("type"))){
            return dataReceiveLog.getMessage();
        }else{
            return dataReceiveLog.getError();
        }
    }
    /**
     * @param params
     * @Description: 查询错误的数据接收日志信息，达到预警临界值时，返回，未达到返回空
     * @Title: getDataReceiveErrorLog
     * @return: Object
     * @author: zx
     * @date:2024年6月6日 上午10:50:55
     */
    @RequestMapping(value = "/getDataReceiveErrorLog")
    @ResponseBody
    public Object getDataReceiveErrorLog(@RequestParam Map<String, Object> params) {
        Wrapper<DataReceiveLogEntity> wrapper = new EntityWrapper<DataReceiveLogEntity>();
        wrapper.isNotNull("error");
        wrapper.eq("is_read", 0);
        List<DataReceiveLogEntity> result = this.dataReceiveLogService.selectList(wrapper);
        //获取预警临界值
        Integer trigger = Integer.parseInt(CdcConfigCache.me().getValueByKey("SDS", "DataReceiveTrigger"));
        if(CommonUtil.isNotEmpty(result) && result.size()>=trigger){
            return result;
        }else{
            return null;
        }
    }
    /**
     * @param params
     * @Description: 用户点击查看时，更新数据接收日志的状态
     * @Title: updateDataReceiveLogReadStatus
     * @return: Object
     * @author: zx
     * @date:2024年6月6日 上午10:50:55
     */
    @RequestMapping(value = "/updateDataReceiveLogReadStatus")
    @ResponseBody
    public Object updateDataReceiveLogReadStatus(@RequestParam Map<String, Object> params) {
        if (CommonUtil.isEmpty(params.get("logIds"))) {
            return false;
        }
        JSONArray logIds = JSONArray.parseArray(String.valueOf(params.get("logIds")));
        //将本次提示的错误日志和正确日志 设置为已读状态
        return this.dataReceiveLogService.updateForSet("is_read=1", new EntityWrapper<DataReceiveLogEntity>().in("ID", logIds).or().isNull("error"));
    }

}
